<template>
<div class="box">
    <el-form :model="pwdForm" status-icon :rules="rules" ref="pwdForm" label-width="100px" label-suffix="：" class="demo-ruleForm">
        <el-form-item label="旧密码" prop="oldPass">
            <el-input type="password" v-model="pwdForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
            <el-input type="password" v-model="pwdForm.newPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkNewPass">
            <el-input type="password" v-model="pwdForm.checkNewPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="modify">提交</el-button>
            <el-button @click="resetForm('pwdForm')">重置</el-button>
        </el-form-item>
    </el-form>
    </div>
</template>
<script>
  import { axiosRequest } from "@/utils/axiosRequest";
  import { Base64 } from 'js-base64';
  export default {
    data() {
      var validateOldPass = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("请输入旧密码"));
        } else {
          callback();
        }
      };
      var validateNewPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.pwdForm.checkNewPass !== '') {
            this.$refs.pwdForm.validateField('checkNewPass');
          }
          callback();
        }
      };
      var validateCheckNewPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.pwdForm.newPass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        pwdForm: {
          oldPass: '',
          newPass: '',
          checkNewPass: ''
        },
        rules: {
          newPass: [
            { validator: validateNewPass, trigger: 'blur' }
          ],
          checkNewPass: [
            { validator: validateCheckNewPass, trigger: 'blur' }
          ],
          oldPass: [
            { validator: validateOldPass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      modify() {
        this.$refs.pwdForm.validate((valid) => {
          if (valid) {
            let pwdInfo = {
              oldPwd: Base64.encode(this.pwdForm.oldPass),
              newPwd: Base64.encode(this.pwdForm.newPass)
            }
            axiosRequest("post", "/modify-pwd", pwdInfo).then((res) => {
              if (res.data.code === 200) {
                this.$message(res.data.data);
                this.$router.push({ name: "Login" });
              } else {
                this.$message.error(res.data.data);
              }
            });
          } else {
            this.$message.error("校验不通过，提交失败！");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="less" scoped>
  .box{
    padding-left: 10px;
    width: 100%;
    height: 100%;
  }

  .demo-ruleForm {
    width: 25%;
  }

  .el-button {
    margin-left: 20px;
  }
</style>
